package com.news.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.news.entity.News;
import com.news.util.PageModel;

public class NewsDaoImpl implements NewsDao {

	private SessionFactory session;

	@Override
	public void setSession(SessionFactory session) {
		this.session = session;
	}

	@Override
	public PageModel getPageModel(Integer currentPage, Integer rows) {
		Long totalsl = getAllCount();
		int totals = Integer.parseInt(totalsl.toString());
		List<News> datas = getAllNews(currentPage, rows);
		return new PageModel(totals, datas, rows, currentPage);
	}

	@Override
	public Long getAllCount() {
		return (Long) session.getCurrentSession()
				.createQuery("select count(*) from News").uniqueResult();
	}

	@Override
	public List<News> getAllNews(int currPage, int pageSize) {
		StringBuffer sb = new StringBuffer();
		sb.append("from News as lo");
		Query query = session.getCurrentSession().createQuery(sb.toString());
		query.setFirstResult((currPage - 1) * pageSize);
		query.setMaxResults(pageSize);
		return query.list();
	}

	@Override
	public PageModel getPageModelByPid(Integer currentPage, Integer rows,
			int pid) {
		Long totalsl = getAllCount();
		int totals = Integer.parseInt(totalsl.toString());
		List<News> datas = getAllNewsByPid(currentPage, rows, pid);
		return new PageModel(totals, datas, rows, currentPage);
	}

	@Override
	public Long getAllCountByPid(int pid) {
		return (Long) session.getCurrentSession()
				.createQuery("select count(*) from News where p_id=?")
				.setParameter(0, pid).uniqueResult();
	}

	@Override
	public List<News> getAllNewsByPid(int currPage, int pageSize, int pid) {
		StringBuffer sb = new StringBuffer();
		sb.append("from News as lo where p_id=?");
		Query query = session.getCurrentSession().createQuery(sb.toString());
		query.setFirstResult((currPage - 1) * pageSize);
		query.setMaxResults(pageSize);
		query.setInteger(0, pid);
		return query.list();
	}

	// addnews
	@Override
	public void addNews(News news) {
		session.getCurrentSession().save(news);
	}

	@Override
	public boolean delUser(int newsid) {
		String hql = "delete News u where u.id = ?";
		Query query = session.getCurrentSession().createQuery(hql);
		query.setInteger(0, newsid);

		return (query.executeUpdate() > 0);
	}

	@Override
	public News getNews(int newsid) {
		String hql = "from News u where u.id=?";
		Query query = session.getCurrentSession().createQuery(hql);
		query.setInteger(0, newsid);

		return (News) query.uniqueResult();
	}

	@Override
	public boolean updateNews(News news) {
		String hql = "update News n set n.title=?,n.content=?,n.urlpicture=?,n.c_id=? where n.id =?";
		Query query = session.getCurrentSession().createQuery(hql);
		query.setString(0, news.getTitle());
		query.setString(1, news.getContent());
		query.setString(2, news.getUrlpicture());
		query.setInteger(3, news.getC_id());
		query.setInteger(4, news.getId());

		return (query.executeUpdate() > 0);
	}

}
